<!DOCTYPE html>
<html>
<head><meta name="generator" content="Hexo 3.9.0">
    <title>Choway Blog</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="description" content="七尘的个人博客">
    <meta name="keywords" content="java、python、SpringBoot、Hibernate">
    <link rel="shortcut icon" href="/images/favicon.ico">
    <link rel="stylesheet" href="/css/animate.min.css">
    <link rel="stylesheet" href="/css/bootstrap.min.css">
    <link rel="stylesheet" href="/css/font-awesome.min.css">
    <link rel="stylesheet" href="/css/kratos.diy.css">
    <link rel="stylesheet" href="/css/layer.min.css">
    <link rel="stylesheet" href="/css/superfish.min.css">
    <link rel="stylesheet" href="/css/style.css">
    <script src="/js/jquery.min.js"></script>
    <script src="/js/bootstrap.min.js"></script>
    <script src="/js/hoverIntent.min.js"></script>
    <script src="/js/jquery.easing.min.js"></script>
    <script src="/js/jquery.qrcode.min.js"></script>
    <script src="/js/jquery.stellar.min.js"></script>
    <script src="/js/jquery.waypoints.min.js"></script>
    <script src="/js/kratos.diy.js"></script>
    <script src="/js/kratos.js"></script>
    <script src="/js/layer.min.js"></script>
    <script src="/js/modernizr.min.js"></script>
    <script src="/js/superfish.js"></script>
    <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/styles/atom-one-dark.min.css">
    <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.13.1/highlight.min.js"></script>
    <script type="text/javascript">
      hljs.initHighlightingOnLoad();
    </script>
</head>


<body data-spy="scroll" data-target=".scrollspy">
    <div id="kratos-wrapper">
        <div id="kratos-page">
            <div id="kratos-header">
    <header id="kratos-header-section">
        <div class="container">
            <div class="nav-header">
                <a href="#" class="js-kratos-nav-toggle kratos-nav-toggle"><i></i></a>
                <h1 id="kratos-logo"><a href="/">Choway Blog</a></h1>
                
                <nav id="kratos-menu-wrap">
                  <ul id="kratos-primary-menu" class="sf-menu">
                  
                    <li>
                      <a href="/categories/Java/">Java</a>
                    </li>
                  
                    <li>
                      <a href="/categories/DevOps/">DevOps</a>
                    </li>
                  
                  </ul>
                </nav>
            </div>
        </div>
    </header>
</div>

<div class="kratos-start kratos-hero-2 kratos-post-header">
    <div class="kratos-overlay kratos-post" style="background:#222831"></div>
</div>
            
            <div id="kratos-blog-post" style="background:#f5f5f5;">
                <div class="container">
                    <div class="row">
                        <section id="main" class="col-md-8">
                            <!--  文章模板  -->
<article>
    <div class="kratos-hentry kratos-post-inner clearfix">
        <header class="kratos-entry-header">
            <h1 class="kratos-entry-title text-center">Java 多线程由浅入深</h1>
            <div class="kratos-post-meta text-center">
                <span>
                    <i class="fa fa-commenting-o"></i> choway
                    <i class="fa fa-calendar"></i> 2020-10-23 01:46:34
                    <i class="fa fa-eye"></i> 
                    
                        <a class="category-link" href="/categories/Java/">Java</a>
                    
                </span>
            </div>
        </header>
        <div class="kratos-post-content">
            <a id="more"></a>
<h2 id="线程实现方式"><a href="#线程实现方式" class="headerlink" title="线程实现方式"></a>线程实现方式</h2><ul>
<li><p>继承 Thread 类</p>
<pre><code class="java">public class MyThread extends Thread {

  @Override
  public void run() {
      System.out.println(Thread.currentThread().getName() + &quot; : run task.&quot;);
  }

  public static void main(String[] args) {
      new MyThread().start();
  }
}
</code></pre>
</li>
<li><p>实现 Runnable 接口</p>
<pre><code class="java">public class MyThread implements Runnable {

  @Override
  public void run() {
      System.out.println(Thread.currentThread().getName() + &quot; : run task.&quot;);
  }

  public static void main(String[] args) {
      new Thread(new MyThread()).start();

      // lambda
      new Thread(() -&gt; {
          System.out.println(Thread.currentThread().getName() + &quot; : run task.&quot;);
      }).start();
  }
}
</code></pre>
</li>
<li><p>实现 Callable 接口</p>
<pre><code class="java">public class MyThread implements Callable&lt;Integer&gt; {

  @Override
  public Integer call() throws Exception {
      System.out.println(Thread.currentThread().getName() + &quot; : run task.&quot;);
      return 100;
  }

  public static void main(String[] args) throws Exception {
      Integer result = new MyThread().call();
      System.out.println(result);
  }
}
</code></pre>
<h2 id="Java-线程状态"><a href="#Java-线程状态" class="headerlink" title="Java 线程状态"></a>Java 线程状态</h2></li>
<li>NEW</li>
<li>RUNNABLE</li>
<li>BLOCKED</li>
<li>WAITING</li>
<li>TIMED_WAITING</li>
<li>TERMINATED</li>
</ul>
<h2 id="线程同步"><a href="#线程同步" class="headerlink" title="线程同步"></a>线程同步</h2><ul>
<li>CountDownLatch</li>
<li>CyclicBarrier</li>
<li>Semaphore</li>
<li>Exchanger</li>
</ul>
<h2 id="volatile、synchronized、ReentrantLock、ReadWriteLock、Atomic、ThreadLocal"><a href="#volatile、synchronized、ReentrantLock、ReadWriteLock、Atomic、ThreadLocal" class="headerlink" title="volatile、synchronized、ReentrantLock、ReadWriteLock、Atomic、ThreadLocal"></a>volatile、synchronized、ReentrantLock、ReadWriteLock、Atomic、ThreadLocal</h2><h2 id="AQS-和-CAS-原理"><a href="#AQS-和-CAS-原理" class="headerlink" title="AQS 和 CAS 原理"></a>AQS 和 CAS 原理</h2><h2 id="死锁"><a href="#死锁" class="headerlink" title="死锁"></a>死锁</h2><ul>
<li>什么是死锁</li>
<li>死锁的4个条件</li>
<li>避免死锁的方法</li>
<li>写一个死锁<br>思路：定义2个list，分别加上锁A、锁B，线程1拿住了锁list1，请求锁list2，线程2拿住了锁list2，请求锁list1，在等待对方释放锁的过程中谁也不释放锁<pre><code class="java">
</code></pre>
</li>
</ul>
<h2 id="线程池-ThreadPoolExecutor"><a href="#线程池-ThreadPoolExecutor" class="headerlink" title="线程池 ThreadPoolExecutor"></a>线程池 ThreadPoolExecutor</h2><p>创建线程池可以使用 ThreadPoolExecutor，该类的构造器有7个核心参数：</p>
<ul>
<li>corePoolSize：核心线程数。即使处于空闲状态也要保留在池中的线程数；</li>
<li>maximumPoolSize：线程池中允许的最大线程数；</li>
<li>keepAliveTime：非核心线程空闲存活的时间；</li>
<li>unit：线程空闲存活的时间单位；</li>
<li>workQueue：保存已提交但未执行任务的阻塞队列；</li>
<li>threadFactory：创建新线程的工厂；</li>
<li>handler：拒绝策略<ul>
<li></li>
</ul>
</li>
</ul>
<h2 id="线程池执行流程"><a href="#线程池执行流程" class="headerlink" title="线程池执行流程"></a>线程池执行流程</h2><h2 id="线程池的5种工作队列"><a href="#线程池的5种工作队列" class="headerlink" title="线程池的5种工作队列"></a>线程池的5种工作队列</h2><ul>
<li>ArrayBlockingQueue</li>
<li>LinkedBlockingQueue</li>
<li>DelayQueue</li>
<li>priorityBlockingQueue</li>
<li>SynchronousQueue</li>
</ul>
<h2 id="Executors-常用的4中线程池"><a href="#Executors-常用的4中线程池" class="headerlink" title="Executors 常用的4中线程池"></a>Executors 常用的4中线程池</h2><h2 id="线程池状态"><a href="#线程池状态" class="headerlink" title="线程池状态"></a>线程池状态</h2><h2 id="面试题"><a href="#面试题" class="headerlink" title="面试题"></a>面试题</h2><ul>
<li>Runnable 接口和 Callable 接口的区别<br>Runnable 的 run 方法是 void，只是纯粹的执行 run 方法体的代码；<br>Callable 的 call 方法是有返回值的，会返回一个泛型。和 Future、FutureTask 配合可以获取异步执行的结果；</li>
</ul>

        </div>
        <footer class="kratos-entry-footer clearfix">
            <div class="post-like-donate text-center clearfix" id="post-like-donate">

            </div>
            <div class="footer-tag clearfix">
                <div class="pull-left">
                    <i class="fa fa-tags"></i>
                    
                        <a class="tag-link" href="/tags/java/">java</a>
                    
                </div>
            </div>
        </footer>
    </div>
    <div class="kratos-hentry kratos-copyright text-center clearfix">
        <img alt="知识共享许可协议" src="/images/licenses.png">
        <h5>本作品采用 <a rel="license nofollow" target="_blank" href="http://creativecommons.org/licenses/by-sa/4.0/">知识共享署名-相同方式共享 4.0 国际许可协议</a> 进行许可</h5>
    </div>

    
    <nav class="navigation post-navigation clearfix" role="navigation">
        
        
        <div class="nav-next">
            <a title="springcloud 服务消费 feign" href="/springcloud-feign.html">下一篇 &gt;</a>
        </div>
        
    </nav>
    
</article>
                        </section>

                        <aside id="kratos-widget-area" class="col-md-4 hidden-xs hidden-sm scrollspy">
                            <div id="sidebar">
    <aside id="kratos_about-2" class="widget amadeus_about clearfix">
        <div class="photo-background">
            <div class="photo-background" style="background:url(/images/about_bg.jpg) no-repeat center center; -webkit-background-size: cover; -moz-background-size: cover; -o-background-size: cover; background-size: cover;"></div>
        </div>
        <div class="photo-wrapper clearfix">
            <div class="photo-wrapper-tip text-center">
                <a href="/"><img class="about-photo" src="/images/about_me.jpg" /></a>
            </div>
        </div>
        <div class="textwidget">
            <p style="text-align: center;font-weight:400;">Choway Blog<br />七尘的个人博客</p>
        </div>
    </aside>

    
    <aside class="widget clearfix">
        <h4 class="widget-title">文章目录</h4>
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#线程实现方式"><span class="toc-text">线程实现方式</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Java-线程状态"><span class="toc-text">Java 线程状态</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#线程同步"><span class="toc-text">线程同步</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#volatile、synchronized、ReentrantLock、ReadWriteLock、Atomic、ThreadLocal"><span class="toc-text">volatile、synchronized、ReentrantLock、ReadWriteLock、Atomic、ThreadLocal</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#AQS-和-CAS-原理"><span class="toc-text">AQS 和 CAS 原理</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#死锁"><span class="toc-text">死锁</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#线程池-ThreadPoolExecutor"><span class="toc-text">线程池 ThreadPoolExecutor</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#线程池执行流程"><span class="toc-text">线程池执行流程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#线程池的5种工作队列"><span class="toc-text">线程池的5种工作队列</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#Executors-常用的4中线程池"><span class="toc-text">Executors 常用的4中线程池</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#线程池状态"><span class="toc-text">线程池状态</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#面试题"><span class="toc-text">面试题</span></a></li></ol>
    </aside>
    

    
    <aside class="widget clearfix">
        <h4 class="widget-title">文章分类</h4>
        <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/DevOps/">DevOps</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/DevOps/ActiveMQ/">ActiveMQ</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/DevOps/Docker/">Docker</a></li></ul></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java/">Java</a><ul class="category-list-child"><li class="category-list-item"><a class="category-list-link" href="/categories/Java/JVM/">JVM</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java/SpringBoot/">SpringBoot</a></li><li class="category-list-item"><a class="category-list-link" href="/categories/Java/SpringCloud/">SpringCloud</a></li></ul></li></ul>
    </aside>
    

    
    <aside class="widget clearfix">
        <h4 class="widget-title">标签聚合</h4>
        <div class="tag_clouds">
            <a class="tag-link" href="/tags/activemq/">activemq</a><a class="tag-link" href="/tags/centos/">centos</a><a class="tag-link" href="/tags/docker/">docker</a><a class="tag-link" href="/tags/hotspot/">hotspot</a><a class="tag-link" href="/tags/java/">java</a><a class="tag-link" href="/tags/jms/">jms</a><a class="tag-link" href="/tags/jvm/">jvm</a><a class="tag-link" href="/tags/redis/">redis</a><a class="tag-link" href="/tags/spring-cloud/">spring cloud</a><a class="tag-link" href="/tags/springboot/">springboot</a>
        </div>
    </aside>
    

</div>
                        </aside>
                    </div>
                </div>
            </div>

            <footer>
    <div id="footer">
        <div class="cd-tool visible-lg text-center">
            <a class="cd-top cd-is-visible cd-fade-out"><span class="fa fa-chevron-up"></span></a>
        </div>
        <div class="container">
            <div class="row">
                <div class="col-md-6 col-md-offset-3 footer-list text-center">
                    <p class="kratos-social-icons">
                        <a target="_blank" rel="nofollow" href="https://github.com/choway"><i class="fa fa-github"></i></a>
                    </p>
                    <p>Copyright 2020 <a href="/">Choway Blog</a>. All Rights Reserved.
                </div>
            </div>
        </div>
    </div>
</footer>
        </div>
    </div>
</body>
</html>